package com.soda.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.soda.bean.Menu;
import com.soda.bean.Role;
import com.soda.dao.BaseDao;
import com.soda.dao.RoleDao;
import com.soda.util.PageInfo;
import com.soda.util.SelectRoleInfo;

public class RoleDaoImpl extends BaseDao implements RoleDao{

	/*
	 * 由id获取role
	 * @see com.soda.dao.RoleDao#getRoleById(int)
	 */
	public Role getRoleById(int id) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="select * from role where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, id);
			res=pt.executeQuery();
			if(res.next()){
				return new Role(res.getInt(1), res.getString(2), res.getInt(3),res.getString(4), res.getInt(5));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		return null;
	}

	public List<Role> getAllRole() {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="select * from role where rol_delete=1 order by rol_id desc";
			pt=conn.prepareStatement(sql);
			List<Role> list=new ArrayList<Role>();
			
			res=pt.executeQuery();
			while(res.next()){
				list.add(new Role(res.getInt(1), res.getString(2), res.getInt(3), res.getString(4), res.getInt(5)));
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return null;
	}

	public int deleteFmenutoRole(int rolId) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="delete from role_fmenu where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, rolId);
			
			return pt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return 0;
	}

	public int deleteMenuToRole(int rolId) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="delete from role_menu where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, rolId);
			
			return pt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return 0;
	}

	public int editRoleToFmenu(int rolId, int i) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="insert into role_fmenu values(?,?)";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, rolId);
			pt.setInt(2, i);
			
			return pt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return 0;
	}

	public void addRoleToMenu(int rolId, int parseInt) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="insert into role_menu values(?,?)";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, rolId);
			pt.setInt(2, parseInt);
			
			pt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
	}

	public int editRoleInfo(int rolId,String rolName, int rolState, String rolRemark) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="update role set rol_name=?,rol_state=?,rol_remark=? where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setString(1, rolName);
			pt.setInt(2, rolState);
			pt.setString(3, rolRemark);
			pt.setInt(4, rolId);
			
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		return 0;
	}

	public void editState(int id, int state) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="update role set rol_state=? where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, state);
			pt.setInt(2, id);
			
			pt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
	}

	public int deleteById(int id) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="update role set rol_delete=0 where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, id);
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		
		return 0;
	}

	public int getMaxId() {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="select max(rol_id) from role where rol_delete=1";
			pt=conn.prepareStatement(sql);
			
			res=pt.executeQuery();
			if(res.next()){
				return res.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		return 0;
	}

	public int getAllCount(SelectRoleInfo sri) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			StringBuffer sql=new StringBuffer("select count(1) from role where 1=1 and rol_delete=1");
			if(sri!=null){
				if(sri.getName()!=null){
					sql.append(" and rol_name like ?");
				}
				if(sri.getState()!=null){
					if(sri.getState()!=2){
						sql.append(" and rol_state=?");
					}
				}
			}
			
			pt=conn.prepareStatement(sql.toString());
			int x=1;
			if(sri!=null){
				if(sri.getName()!=null){
					pt.setString(x, sri.getName()+"%");
					x++;
				}
				if(sri.getState()!=null){
					if(sri.getState()!=2){
						pt.setInt(x, sri.getState());
						x++;
					}
				}
			}
			res=pt.executeQuery();
			
			if(res.next()){
				return res.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return 0;
	}

	public List<Role> getAllRoleBySelectInfo(SelectRoleInfo sri, PageInfo pi) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			StringBuffer sql=new StringBuffer("select * from role where 1=1 and rol_delete=1");
			if(sri!=null){
				if(sri.getName()!=null){
					sql.append(" and rol_name like ?");
				}
				if(sri.getState()!=null){
					if(sri.getState()!=2){
					sql.append(" and rol_state=?");
					}
				}
			}
			sql.append(" order by rol_id desc limit ?,?");
			pt=conn.prepareStatement(sql.toString());
			int x=1;
			if(sri!=null){
				if(sri.getName()!=null){
					pt.setString(x, sri.getName()+"%");
					x++;
				}
				if(sri.getState()!=null){
					if(sri.getState()!=2){
					pt.setInt(x, sri.getState());
					x++;
					}
				}
			}
			pt.setInt(x, (pi.getPage()-1)*pi.getPer());
			x++;
			pt.setInt(x, pi.getPer());
			
			res=pt.executeQuery();
			List<Role> list=new ArrayList<Role>();
			while(res.next()){
				list.add(new Role(res.getInt(1), res.getString(2), res.getInt(3), res.getString(4), res.getInt(5)));
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		return null;
	}

	public int ajaxrolename(String rolname) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="select * from role where rol_name=?";
			pt=conn.prepareStatement(sql);
			pt.setString(1, rolname);
			res=pt.executeQuery();
			if(res.next()){
				return 1;
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return 0;
	}

	public String getRoleNameById(int id) {
		// TODO Auto-generated method stub
		try {
			conn=getConnection();
			String sql="select rol_name from role where rol_id=?";
			pt=conn.prepareStatement(sql);
			pt.setInt(1, id);
			res=pt.executeQuery();
			if(res.next()){
				return res.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		return null;
	}

	@Override
	public List<Role> getAllRoleBySelectInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
